package com.koron.css2.serviceManage.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.css2.ServerInterface;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.ServerAreaBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.mapper.PubServerAreaMapper;
import com.koron.util.Constant;
import com.koron.util.JsonUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;
import java.util.Map;

/**
 * 服务区域配置
 * @author 甄玉晴
 *
 */
public class ServerAreaList implements ServerInterface {
	private static Logger logger = LogManager.getLogger(ServerAreaList.class);

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {

		@SuppressWarnings("rawtypes")
		MessageBean<PageInfo> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", PageInfo.class);
		 try {
			 PubServerAreaMapper mapper=factory.getMapper(PubServerAreaMapper.class);
			 	@SuppressWarnings("unchecked")
				Map<String,Object> params = JsonUtils.objectToPojo(req.getData(), Map.class);
			 	 int page = Integer.parseInt(params.get("page").toString());
			     int pageCount = Integer.parseInt(params.get("pageCount").toString());
			     params.put("group", userInfo.getCurWaterCode());
			     PageHelper.startPage(page, pageCount);
			    List<ServerAreaBean> list = mapper.select(params);
			    info.setData(new PageInfo<>(list));
	        } catch (Exception e) {
	            info.setCode(Constant.MESSAGE_INT_FAIL);
	            info.setDescription(e.getMessage());
	            logger.error(e.getMessage(), e);
	        }
	        return info;
	}

}
